import ls from '@/util/ls'
import {getBindInfo } from '@/api/index'
import { phone } from '@/util/config'
export default {
	namespaced:true, //解决命名冲突
	state:{
		communityId:-1,
		communityName:"",
		identityOptions:[
			{ label: '业主', value: 1 },
			{ label: '家属', value: 2 },
			{ label: '租客', value: 3 }
		],
		boundHouses:0, //绑定房子的数量
		location:{
			city:"",
			province:"",
			provinceCode:"",
			cityCode:""
		},
		AreasInfo:{}//全国的地图信息
 
	},
	mutations:{
		setLocation(state,location){
			console.log("setLocation",location)
			state.location=location
		},
		setCommunityId(state,communityId){
			
			if(communityId){
				ls.set("communityId",communityId)
				state.communityId=communityId
			}
			
		},
		setCommunityName(state,communityName){
			ls.set("communityName",communityName)
			state.communityName=communityName
		},
		setBoundHouses(state,boundHouses){
			state.boundHouses=boundHouses
		},
		setAreasInfo(state,AreasInfo){
			state.AreasInfo=AreasInfo
		},
		setCommunityInfo(state,communityInfo){
			state.communityId=communityInfo.communityId
			state.communityName=communityInfo.communityName
		
		}
	},
	actions: {
		checkLoginNotHouse({ state, rootState }) {
			const loginStatus = rootState.app.loginStatus;
			const  communityId=state.communityId
		  console.log("loginStatus",loginStatus)
			if(communityId==-1||!communityId){
				uni.showModal({
					title:"选择小区",
					content:"您还未选择小区，是否去选择小区？",
					success:({confirm})=>{
						if(confirm){
							uni.navigateTo({
								url:"pages/choosecommunity/index"
							})
						}
					}
				})
				return false
			}
			if(loginStatus==0){
					uni.showModal({
						title:"请先登录",
						content:"您还未登录，是否去登录？",
						success:({confirm})=>{
							if(confirm){
								uni.navigateTo({
									url:"pages/login/index"
								})
							}
						}
					})
					return false
			}
			return true
			
		},
		 async checkLoginAndBoundHouses({ state, rootState }) {
			const loginStatus = rootState.app.loginStatus;
		
			const  communityId=state.communityId
			if(communityId==-1||!communityId){
				uni.showModal({
					title:"选择小区",
					content:"您还未选择小区，是否去选择小区？",
					success:({confirm})=>{
						if(confirm){
							uni.navigateTo({
								url:"pages/choosecommunity/index"
							})
						}
					}
				})
				return false
			}
			
			if(loginStatus==0){
					uni.showModal({
						title:"请先登录",
						content:"您还未登录，是否去登录？",
						success:({confirm})=>{
							if(confirm){
								uni.navigateTo({
									url:"pages/login/index"
								})
							}
						}
					})
					return false
			}else if(state.boundHouses==0){
				const houseCount = await this.dispatch('community/getHouseList')
				const reviewHouseCount = await this.dispatch('community/getreviewHouseList')
				if(houseCount > 0){
					state.boundHouses = houseCount
					return true
				}else if(reviewHouseCount > 0){
					uni.showModal({
						title:"提示",
						content:"您已申请绑定房屋审核中，请耐心等待。您可以在我的-我的房屋，进行查看审核状态，或联系客服紧急处理："+phone,
						confirmText:"确定",
						cancelText:"取消",
						success:({confirm})=>{
							// if(confirm){
							// 	uni.makePhoneCall({
							// 		phoneNumber: phone
							// 	})
							// }
						}
					})
					return false
				} else{
					uni.showModal({
						title:"您还未绑定房屋",
						content:"您还未绑定房屋，是否去绑定？",
						success:({confirm})=>{
							if(confirm){
								uni.navigateTo({
									url:"/pages/bindhouse/index?statue=jumpto"
								})
							}
						}
					})
					return false
				}
				
		
			}
			return true
			
		},
		async getHouseList({ state, rootState }) { //已经审核过
			const  communityId=state.communityId
			const  wxUserId=rootState.app.user.wxUser.wxUserId
			const res=await getBindInfo({
				communityId:communityId,
				liveUserId:wxUserId,
				auditStatus:2
			})
			return res.rows.length
		},
		async getreviewHouseList({ state, rootState }) { //审核中
			const  communityId=state.communityId
			const  wxUserId=rootState.app.user.wxUser.wxUserId
			const res=await getBindInfo({
				communityId:communityId,
				liveUserId:wxUserId,
				auditStatus:1
			})
			return res.rows.length
		}
	}
}